package com.own.component.util.time;

import java.time.Duration;

/**
 * DurationUtil
 *
 * @author chenxueli
 * @date 2024-01-18 15:54:56
 */
public class DurationUtil {

    /**
     * 格式化时间
     *
     * @param duration 时间
     * @return 格式化后的时间
     */
    public static String format(Duration duration) {
        duration = duration.abs();
        var builder = new StringBuilder();
        var day = duration.toDaysPart();
        if (day != 0) {
            builder.append(day).append("天");
        }
        var hour = duration.toHoursPart();
        if (hour != 0 || !builder.isEmpty()) {
            builder.append(hour).append("小时");
        }
        var minute = duration.toMinutesPart();
        if (minute != 0 || !builder.isEmpty()) {
            builder.append(minute).append("分钟");
        }
        var second = duration.toSecondsPart();
        if (second != 0 || !builder.isEmpty()) {
            builder.append(second).append("秒");
        }
        return builder.toString();
    }

}
